/**
 ******************************************************************************
 * @file           : bsp_timer_config.h
 * @brief          : Timer Configuration File (STM32F4xx)
 ******************************************************************************
 * @attention
 *
 * 定时器配置文件
 * 支持基本定时中断和PWM输出
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BSP_TIMER_CONFIG_H
#define __BSP_TIMER_CONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/* ============================================================================ */
/*                       日志系统配置（改这里的数字）                             */
/* ============================================================================ */
/* 日志后端选择：1=EasyLogger, 2=printf, 0=关闭 */
#define TIMER_USE_LOG     1

/* 根据配置自动定义日志宏 */
#if (TIMER_USE_LOG == 1)
    /* 使用EasyLogger */
    #ifndef LOG_TAG
        #define LOG_TAG    "TIMER"
    #endif
    #include "elog.h"
    
#elif (TIMER_USE_LOG == 2)
    /* 使用printf */
    #include <stdio.h>
    #define log_i(fmt, ...)     printf(fmt "\r\n", ##__VA_ARGS__)
    #define log_w(fmt, ...)     printf("[WARN] " fmt "\r\n", ##__VA_ARGS__)
    #define log_e(fmt, ...)     printf("[ERROR] " fmt "\r\n", ##__VA_ARGS__)
    #define log_d(fmt, ...)     printf("[DEBUG] " fmt "\r\n", ##__VA_ARGS__)
    
#else
    /* 关闭日志 */
    #define log_i(fmt, ...)
    #define log_w(fmt, ...)
    #define log_e(fmt, ...)
    #define log_d(fmt, ...)
#endif

/* ============================================================================ */
/*                      定时器选择配置（改这里即可切换定时器）                      */
/* ============================================================================ */
/**
 * @brief 定时器选择（2~14）
 * 
 * STM32F4 定时器资源：
 *   TIM1: 高级定时器（APB2，最大168MHz）
 *   TIM2~TIM5: 通用定时器（APB1，最大84MHz，32位）
 *   TIM6~TIM7: 基本定时器（APB1，仅定时）
 *   TIM8: 高级定时器（APB2）
 *   TIM9~TIM14: 通用定时器（APB2）
 * 
 * 推荐使用TIM2~TIM5（功能全，32位计数器）
 */
#define TIMER_SELECT_NUM            2       /*!< 选择使用的定时器号 */

/* ============================================================================ */
/*                    自动配置区域（请勿手动修改）                                */
/* ============================================================================ */

/* --------------------------------------------------------------------------- */
/* TIM2 配置 (APB1, 32位)                                                     */
/* --------------------------------------------------------------------------- */
#if (TIMER_SELECT_NUM == 2)
    #define TIMER_TIMx                      TIM2
    #define TIMER_TIMx_CLK_ENABLE()         __HAL_RCC_TIM2_CLK_ENABLE()
    #define TIMER_TIMx_IRQn                 TIM2_IRQn
    #define TIMER_TIMx_IRQHandler           TIM2_IRQHandler
    #define TIMER_APB_CLOCK                 84000000    /* APB1=84MHz */

/* --------------------------------------------------------------------------- */
/* TIM3 配置 (APB1, 16位)                                                     */
/* --------------------------------------------------------------------------- */
#elif (TIMER_SELECT_NUM == 3)
    #define TIMER_TIMx                      TIM3
    #define TIMER_TIMx_CLK_ENABLE()         __HAL_RCC_TIM3_CLK_ENABLE()
    #define TIMER_TIMx_IRQn                 TIM3_IRQn
    #define TIMER_TIMx_IRQHandler           TIM3_IRQHandler
    #define TIMER_APB_CLOCK                 84000000

/* --------------------------------------------------------------------------- */
/* TIM4 配置 (APB1, 16位)                                                     */
/* --------------------------------------------------------------------------- */
#elif (TIMER_SELECT_NUM == 4)
    #define TIMER_TIMx                      TIM4
    #define TIMER_TIMx_CLK_ENABLE()         __HAL_RCC_TIM4_CLK_ENABLE()
    #define TIMER_TIMx_IRQn                 TIM4_IRQn
    #define TIMER_TIMx_IRQHandler           TIM4_IRQHandler
    #define TIMER_APB_CLOCK                 84000000

/* --------------------------------------------------------------------------- */
/* TIM5 配置 (APB1, 32位)                                                     */
/* --------------------------------------------------------------------------- */
#elif (TIMER_SELECT_NUM == 5)
    #define TIMER_TIMx                      TIM5
    #define TIMER_TIMx_CLK_ENABLE()         __HAL_RCC_TIM5_CLK_ENABLE()
    #define TIMER_TIMx_IRQn                 TIM5_IRQn
    #define TIMER_TIMx_IRQHandler           TIM5_IRQHandler
    #define TIMER_APB_CLOCK                 84000000

/* --------------------------------------------------------------------------- */
/* TIM6 配置 (APB1, 基本定时器)                                                */
/* --------------------------------------------------------------------------- */
#elif (TIMER_SELECT_NUM == 6)
    #define TIMER_TIMx                      TIM6
    #define TIMER_TIMx_CLK_ENABLE()         __HAL_RCC_TIM6_CLK_ENABLE()
    #define TIMER_TIMx_IRQn                 TIM6_DAC_IRQn
    #define TIMER_TIMx_IRQHandler           TIM6_DAC_IRQHandler
    #define TIMER_APB_CLOCK                 84000000

/* --------------------------------------------------------------------------- */
/* TIM7 配置 (APB1, 基本定时器)                                                */
/* --------------------------------------------------------------------------- */
#elif (TIMER_SELECT_NUM == 7)
    #define TIMER_TIMx                      TIM7
    #define TIMER_TIMx_CLK_ENABLE()         __HAL_RCC_TIM7_CLK_ENABLE()
    #define TIMER_TIMx_IRQn                 TIM7_IRQn
    #define TIMER_TIMx_IRQHandler           TIM7_IRQHandler
    #define TIMER_APB_CLOCK                 84000000

#else
    #error "Invalid TIMER_SELECT_NUM, please select 2~7"
#endif

/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* __BSP_TIMER_CONFIG_H */
